package com.yami.shop.admin.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Gyl;
import com.yami.shop.bean.model.ProdTag;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.admin.util.SecurityUtils;
import com.yami.shop.service.GylService;
import com.yami.shop.service.ProdTagService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/prod/gyl")
public class GylController {
    @Autowired
    private GylService gylService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param
     * @return 分页数据
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<Gyl>> getGylPage(PageParam<Gyl> page, Gyl gyl) {
        IPage<Gyl> gylPage = gylService.page(
                page, new LambdaQueryWrapper<Gyl>()
                        //.eq(prodTag.getStatus() != null, ProdTag::getStatus, prodTag.getStatus())
                        .like(gyl.getGyl() != null, Gyl::getGyl, gyl.getGyl()) );
                        //.orderByDesc(ProdTag::getSeq, ProdTag::getCreateTime));
        return ServerResponseEntity.success(gylPage);

    }


    /**
     * 通过id查询商品分组标签
     *
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}")
    public ServerResponseEntity<Gyl> getById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(gylService.getById(id));
    }

    /**
     * 新增商品分组标签
     *
     * @  商品分组标签
     * @return 是否新增成功
     */
    @SysLog("新增供应链")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('prod:gyl:save')")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid Gyl gyl) {
        // 查看是否相同的标签
        List<Gyl> list = gylService.list(new LambdaQueryWrapper<Gyl>().like(Gyl::getGyl, gyl.getGyl()));
        if (CollectionUtil.isNotEmpty(list)) {
            throw new YamiShopBindException("供应链已存在，不能添加相同的供应链");
        }
        /* prodTag.setIsDefault(0);
        prodTag.setProdCount(0L);
        prodTag.setCreateTime(new Date());
        prodTag.setUpdateTime(new Date());
        prodTag.setShopId(SecurityUtils.getSysUser().getShopId());
        prodTagService.removeProdTag();*/
        return ServerResponseEntity.success(gylService.save(gyl));
    }

    /**
     * 修改商品分组标签
     *
     * @  商品分组标签
     * @return 是否修改成功
     */
    @SysLog("修改商品分组标签")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('prod:gyl:update')")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid Gyl gyl) {
        //prodTag.setUpdateTime(new Date());
        //prodTagService.removeProdTag();
        return ServerResponseEntity.success(gylService.updateById(gyl));
    }

    /**
     * 通过id删除商品分组标签
     *
     * @param id id
     * @return 是否删除成功
     */
    @SysLog("删除商品分组标签")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('prod:gyl:delete')")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long id) {
        Gyl gyl = gylService.getById(id);

        //prodTagService.removeProdTag();
        return ServerResponseEntity.success(gylService.removeById(id));
    }

    @GetMapping("/listGylList")
    public ServerResponseEntity<List<Gyl>> listGylList() {

        return ServerResponseEntity.success(gylService.list());

    }
}
